﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.SysOrgan;
using System.Collections.Generic;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.Mgoa.Collaboration
{
    public partial class collaboration_pesonal_view : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdId.Value = (Request.QueryString["id"] != null) ? Request.QueryString["id"].ToString() : "";
                
                InitPage();
            }
        }

        private void InitPage()
        {
            string sId = hdId.Value;
            CollaborateBLL oCollaborate = new CollaborateBLL();
            MsCollaborativeTopic message = oCollaborate.GetCollaborativeTopicById(sId);

            List<MsComment> lstComment = message.lstComment;
            int iRecordCount = lstComment.Count;
            hdRecordCount.Value = iRecordCount.ToString();
            if (iRecordCount > 0)
            {
                trSplit1.Visible = true;
                trComment.Visible = true;
            }
            else
            {
                trSplit1.Visible = false;
                trComment.Visible = false;
            }

            if (message.Status == "1")
            {
                trSplit2.Visible = true;
                trWriteBack.Visible = true;
                btnSubmit.Visible = true;
                btnReset.Visible = true;
            }
            else
            {
                trSplit2.Visible = false;
                trWriteBack.Visible = false;
                btnSubmit.Visible = false;
                btnReset.Visible = false;
            }
        }

        [global::System.Web.Services.WebMethod]
        public static string GetCollaborativeCommentInfo(string sCollaborativeId, int iCurPageIndex, int iEveryPageRecCount)
        {
            string sResult = "";
            
            CollaborateBLL oCollaborate = new CollaborateBLL();
            List<MsComment> lstCommentResult = new List<MsComment>();
            List<MsComment> lstComment = oCollaborate.GetCollaborativeComment(sCollaborativeId);
            int iRecordCount = lstComment.Count;
            int iPageCount = (iRecordCount % iEveryPageRecCount == 0) ? iRecordCount / iEveryPageRecCount : (iRecordCount / iEveryPageRecCount) + 1;
            int iBeginIndex = iEveryPageRecCount * (iCurPageIndex - 1);
            int iFinishIndex = iCurPageIndex * iEveryPageRecCount;
            iFinishIndex = (iFinishIndex > iRecordCount) ? iRecordCount : iFinishIndex;
            for (int i = iFinishIndex - 1; i >= iBeginIndex; i--)
            {
                MsComment msComment = lstComment[i];
                lstCommentResult.Add(msComment);
            }
            lstComment = null;
            var oResult = new { PageCount = iPageCount, RecordCount = iRecordCount, Comment = lstCommentResult };
            sResult = JavaScriptConvert.SerializeObject(oResult);
            return sResult;
        }

        protected void lnkAppear_Click(object sender, EventArgs e)
        {
            LoginUser logUser = logUser = (LoginUser)Session["LogUserInfo"];
            string sCollaborativeId=hdId.Value;
            string sContent = txtWriteBackContent.Text.Replace("'", "''");
            string sPublisherId = logUser.UserId;
            string sPublishDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            MsComment msComment = new MsComment();
            msComment.CollaborativeId = sCollaborativeId;
            msComment.Content = sContent;
            msComment.PublisherId = sPublisherId;
            msComment.PublishDate = sPublishDate;
            CollaborateBLL oCollaborate = new CollaborateBLL();
            string sCommentId = oCollaborate.SaveComment(msComment);
            if (sCommentId!="")
            {
                appearAttach.SaveAttaches("0050", sCommentId);
                txtWriteBackContent.Text = "";
                appearAttach.EditShow("0050", "");
                int iRecordCount = Convert.ToInt32(hdRecordCount.Value);
                if (iRecordCount==0)
                    InitPage();
            }
        }
    }
}
